/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.activities.wishlist;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

import com.phonebooksharing.android.R;

public class WishlistSharingContainer extends TabActivity {
    
    public static final String INITIAL_TAB = "i";
//    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        final Resources r = getResources();
        final TabHost th = getTabHost();
        th.addTab(th.newTabSpec("t1")
                .setIndicator("Sharing", r.getDrawable(R.drawable.sharing))
                .setContent(new Intent(this, WishlistSharingActivity.class)));

        th.addTab(th.newTabSpec("t2")
                .setIndicator("Not Sharing", r.getDrawable(R.drawable.sharing))
                .setContent(new Intent(this, WishlistNotSharingActivity.class)));

        th.setCurrentTab(getIntent().getIntExtra(INITIAL_TAB, 0));
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "Add New").setIcon(android.R.drawable.ic_menu_search);
//        menu.add(1, 1, 1, "Add By Voice").setIcon(android.R.drawable.ic_menu_sort_alphabetically);
        return super.onCreateOptionsMenu(menu);
    }
    
//    @Override
//    public boolean onPrepareOptionsMenu(Menu menu) {
//        // Check to see if a recognition activity is present
////        PackageManager pm = getPackageManager();
////        List<ResolveInfo> activities = pm.queryIntentActivities(
////                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
////        
////        MenuItem item;
////        item = menu.findItem(1);
////        item.setEnabled(activities.size() > 0);
//
//        return super.onPrepareOptionsMenu(menu);
//    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        if(item.getItemId() == 0) {
            //onSearchRequested();
        startActivity(new Intent(this, WishlistSearchWrapper.class));
//        }
        return super.onOptionsItemSelected(item);
    }
/*    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == 0) {
            onSearchRequested();
        } else {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
            startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        return new AlertDialog.Builder(this)
        .setTitle("From Voice Recognition")
        .setItems(voiceMatches, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent i = new Intent(WishlistSharingContainer.this, WishlistSearchActivity.class);
                i.setAction(Intent.ACTION_SEARCH);
                i.putExtra(SearchManager.QUERY, voiceMatches[which]);
                startActivity(i);
            }
        })
        .create();
    }
    
    private String[] voiceMatches;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
            // Fill the list view with the strings the recognizer thought it could have heard
            ArrayList<String> list = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            voiceMatches = new String[list.size()];
            voiceMatches = list.toArray(voiceMatches);
            showDialog(0);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
    */
//    @Override
//    public boolean onSearchRequested() {
//        startSearch(null, false, null, false);
//        return true;
//    }
}
